package IHM;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import Commands.CommandEteindre;
import Commands.CommandMetronome;
import Controleur.ControleurImpl;
import Controleur.ObserveurIHM;
import Moteur.IHorloge;

public class InterMetronome extends JFrame implements IIHM {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField txtMarqueBenitmoxx;
	private JTextField txtNbreTemps;
	private JTextField lesMesuresDel;
	private JTextField lesTempsDel;
	private JToggleButton tglbtnOnOff;
	private ControleurImpl controleur;
	private JSlider slider;
	private JLabel lblNewLabel;
	private CommandMetronome start, stop, modifTempo, IncrTemps, DecrTemps;
	private IHorloge horloge;
	private CommandMetronome eteindreLed1, eteindreLed2;



	public InterMetronome() {
		setResizable(false);
		eteindreLed1=new CommandEteindre(1,this);
		eteindreLed2=new CommandEteindre(2,this);
		setTitle("M2 MIAGE");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 667, 259);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JTextField lblAocV = new JTextField("AOC - V1");
		lblAocV.setBounds(5, 202, 651, 24);
		lblAocV.setFont(new Font("Segoe UI", Font.BOLD | Font.ITALIC, 13));
		lblAocV.setHorizontalAlignment(SwingConstants.RIGHT);
		lblAocV.setToolTipText("Premi\u00E8re version du m\u00E9tronome");
		lblAocV.setEditable(false);
		lblAocV.setForeground(Color.WHITE);
		lblAocV.setBackground(Color.BLACK);
		contentPane.add(lblAocV);

		txtMarqueBenitmoxx = new JTextField();
		txtMarqueBenitmoxx.setLocation(5, 5);
		txtMarqueBenitmoxx.setForeground(Color.WHITE);
		txtMarqueBenitmoxx.setBackground(SystemColor.desktop);
		txtMarqueBenitmoxx.setSize(651, 22);
		txtMarqueBenitmoxx.setFont(txtMarqueBenitmoxx.getFont().deriveFont(txtMarqueBenitmoxx.getFont().getStyle() | Font.BOLD | Font.ITALIC, 13f));
		txtMarqueBenitmoxx.setEditable(false);
		txtMarqueBenitmoxx.setHorizontalAlignment(SwingConstants.LEFT);
		txtMarqueBenitmoxx.setToolTipText("La fameuse marque mondialement connue !");
		txtMarqueBenitmoxx.setText("M\u00E9tronome BenitMoxx");
		contentPane.add(txtMarqueBenitmoxx);
		//txtMarqueBenitmoxx.setColumns(10);

		JPanel panelW = new JPanel();
		panelW.setBounds(5, 27, 75, 175);
		contentPane.add(panelW);
		panelW.setLayout(null);

		tglbtnOnOff = new JToggleButton();
		tglbtnOnOff.setBounds(0, 11, 75, 153);
		tglbtnOnOff.setText("On/Off");
		panelW.add(tglbtnOnOff);
		tglbtnOnOff.setActionCommand("on-off");
		tglbtnOnOff.addActionListener(new ActionSwitchButton(this));

		JPanel panelE = new JPanel();
		panelE.setBounds(470, 27, 186, 175);
		contentPane.add(panelE);
		panelE.setLayout(null);

		/*JSpinner spinner = new JSpinner();
		spinner.setToolTipText("Min: 2 - Max: 7");
		spinner.setModel(new SpinnerNumberModel(4, 2, 7, 1));
		panelEN.add(spinner, BorderLayout.EAST);*/
				
						JButton boutonIncr = new JButton("+");
						boutonIncr.setBounds(137, 6, 41, 23);
						panelE.add(boutonIncr);
						boutonIncr.addActionListener(new ActionButton(this));
						boutonIncr.setActionCommand("plus");
				
				JButton boutonDecr = new JButton("-");
				boutonDecr.setBounds(86, 6, 41, 23);
				panelE.add(boutonDecr);
				boutonDecr.addActionListener(new ActionButton(this));
				boutonDecr.setActionCommand("moins");
				lesTempsDel = new JTextField(" ");
				lesTempsDel.setBackground(Color.GRAY);
				lesTempsDel.setBounds(132, 133, 12, 12);
				panelE.add(lesTempsDel);
				lesTempsDel.setEditable(false);
				lesMesuresDel = new JTextField(" ");
				lesMesuresDel.setBackground(Color.GRAY);
				lesMesuresDel.setBounds(132, 64, 12, 12);
				panelE.add(lesMesuresDel);
				lesMesuresDel.setEditable(false);
				
						JTextField lesMesures = new JTextField("    Mesures");
						lesMesures.setBounds(0, 35, 186, 70);
						panelE.add(lesMesures);
						lesMesures.setEditable(false);
						JTextField lesTemps = new JTextField("    Temps");
						lesTemps.setBounds(0, 104, 186, 70);
						panelE.add(lesTemps);
						lesTemps.setEditable(false);
						
								txtNbreTemps = new JTextField();
								txtNbreTemps.setBounds(0, 0, 186, 36);
								panelE.add(txtNbreTemps);
								txtNbreTemps.setEditable(false);
								txtNbreTemps.setText("    Nb Temps");
								txtNbreTemps.setColumns(10);


		JPanel panelC = new JPanel();
		panelC.setBounds(80, 27, 390, 175);
		panelC.setBorder(new EmptyBorder(0, 1, 0, 1));
		panelC.setLayout(new BorderLayout());
		contentPane.add(panelC);

		slider = new JSlider();
		slider.setForeground(Color.BLACK);
		slider.setBorder(new EmptyBorder(0, 1, 0, 1));
		slider.setValue(100);
		slider.setToolTipText("Min: 80 - Max: 200");
		slider.setMinimum(80);
		slider.setMaximum(200);
		slider.addChangeListener(new ActionSlider(this)); 
		panelC.add(slider, BorderLayout.SOUTH);
		
		lblNewLabel = new JLabel("",JLabel.CENTER);
		afficheTempo(slider.getValue());
		panelC.add(lblNewLabel, BorderLayout.CENTER);

	}

	public IHorloge getHorloge() {
		return horloge;
	}

	public void setHorloge(IHorloge horloge) {
		this.horloge = horloge;
	}
	
	public void setControleur(ControleurImpl controleur) {
		this.controleur = controleur;
	}

	public ControleurImpl getControleur() {
		return controleur;
	}

	@Override
	public int getPositionSlider() {
		return slider.getValue();
	}
	
	@Override
	public void afficheTempo(int tempo)
	{
		lblNewLabel.setText("" + tempo);
	}

	@Override
	public void attach(ObserveurIHM o) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void detach(ObserveurIHM o) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public CommandMetronome getCommandStart() {
		return start;
	}

	@Override
	public CommandMetronome getCommandStop() {
		return stop;
	}

	@Override
	public CommandMetronome getCommandIncrTemps() {
		return IncrTemps;
	}

	@Override
	public CommandMetronome getCommandDecrTemps() {
		return DecrTemps;
	}


	@Override
	public CommandMetronome getCommandModifTempo() {
		return modifTempo;
	}

	@Override
	public void setCommandStart(CommandMetronome c) {
		this.start = c;
	}

	@Override
	public void setCommandStop(CommandMetronome c) {
		this.stop = c;
	}

	@Override
	public void setCommandIncrTemps(CommandMetronome c) {
		this.IncrTemps = c;
	}
 
	@Override
	public void setCommandDecrTemps(CommandMetronome c) {
		this.DecrTemps = c;
	}


	@Override
	public void setCommandModifTempo(CommandMetronome c) {
		this.modifTempo = c;
	}

	@Override
	public void allumerLED(int numLED) {
		if(numLED==1){
			lesTempsDel.setBackground(Color.GREEN);
			horloge.activerApresDelai(eteindreLed1, 0.1f);
		}else if(numLED==2){
			lesMesuresDel.setBackground(Color.GREEN);
			horloge.activerApresDelai(eteindreLed2, 0.1f);
		}
	}

	@Override
	public void eteindreLED(int numLED) {
		if(numLED==1){
			lesTempsDel.setBackground(Color.GRAY);
		}else if(numLED==2){
			lesMesuresDel.setBackground(Color.GRAY);
		}
	}


}
